<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Profile extends CI_Controller {

	 var $dbmain;
     function __construct()
	 {
		    parent::__construct();
            include_once "func.php";
            $this->dbmain = $db;
 	 }

 	public function wxreg(){
 		
  		echo("wxreg ok");
 		exit;
 	}


	public function profile_welcome(){

		setcookie('loginfrom','common');
	    header("location: http://www.xunlianri.com/index.php/profile");
	}


 	public function index()
	{
		//获取openid
		$openid = '';
		//去获取openid 
		if(isset($_GET['openid'])){
		    $openid = $_GET['openid'];
		}else{
//			echo("正在与微信连接...");
			$base_uri = urlencode('http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]);
		    $backurl = $base_uri.'?backfrom=indexgetopenid';
		    $this->connectWX($backurl,'snsapi_base'); 
		}
		$from = 'wx';
		if(isset($_GET['from'])){
		    $from = $_GET['from'];
		}
 
		//如果有wx_openid 直接获取member表的信息
		$sql = "select * from member where wx_openid='$openid'";
		$member = $this->dbmain->get_row($sql);
 
		//如果没有 跳转到注册页面
		if(!$member){
			 //$this->load->view('login',$data);
			 header("Location: /index.php/profile/login?wx_openid=$openid&from=$from");
			 exit;

		}else{

			$from = $_GET['from'];

 	        $data = array(
	        	'member' => $member,
	 			'stype' => 'all',
	 			'from' => $from
			);


			// $loginfrom = $_COOKIE["loginfrom"];
			// echo($loginfrom);
			// exit; 
			setcookie('member_id',$member->id);
			//echo('from:'.$from);

			$this->load->view('profile',$data);
		}

 	}

	function connectWX($backurl,$scopetype){
	    $redirect_uri = urlencode("http://www.xunlianri.com/wxoauth2/auth.php");
	    $oauth2_uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8ed1756092833c6a&redirect_uri=$redirect_uri&response_type=code&scope=".$scopetype."&state=1#wechat_redirect";
	    header("location: $oauth2_uri");
	    exit;
	}


 	public function login(){
		$from = 'wx';
		if(isset($_GET['from'])){
		    $from = $_GET['from'];
		}
		$wx_openid = $_GET['wx_openid'];
 	    $data = array(
	        'from' => $from,
	        'wx_openid' => $wx_openid
 		);
		$this->load->view('login',$data);
 	}

 	public function getsmscode4web(){
 		include_once "func_sms.php";

 		$phonenumber = $_POST['phonenumber'];
 		$wx_openid = '';

		if($phonenumber==''){
			$return = array(
			    'result' => 3
			);
			ajax_return($return);
		}
  		
 		if(!preg_match("/1[3458]{1}\d{9}$/",$phonenumber)){  
			$return = array(
			    'result' => 4
			);
			ajax_return($return);
		}

		$smscode = substr(rand(), 0, 4); 
		send_sms( $smscode, $phonenumber);

		$sql = "insert into phonenumber(phonenumber,code,createdate,wx_openid) values('$phonenumber','$smscode',NOW(),'$wx_openid')";
		$res = $this->dbmain->query($sql);
		 
		if($res){
		 	$return = array(
			    'result' => 1,
			    'insert_id' => mysql_insert_id()
			);
			ajax_return($return);
		}

 	}


 	public function getsmscode(){
 		include_once "func_sms.php";

 		$phonenumber = $_POST['phonenumber'];
 		$wx_openid = $_POST['wx_openid'];

		if($phonenumber==''){
			$return = array(
			    'result' => 3
			);
			ajax_return($return);
		}
  		
 		if(!preg_match("/1[3458]{1}\d{9}$/",$phonenumber)){  
			$return = array(
			    'result' => 4
			);
			ajax_return($return);
		}

		$smscode = substr(rand(), 0, 4); 
		send_sms( $smscode, $phonenumber);

		$sql = "insert into phonenumber(phonenumber,code,createdate,wx_openid) values('$phonenumber','$smscode',NOW(),'$wx_openid')";
		$res = $this->dbmain->query($sql);
		 
		if($res){
		 	$return = array(
			    'result' => 1,
			    'insert_id' => mysql_insert_id()
			);
			ajax_return($return);
		}

 	}

 	public function checkcode(){

 		 //验证成功
 	}

	public function orderlist()
	{
		$this->load->view('profile_orderlist');
	}
 

	public function coupon()
	{
		$this->load->view('profile_coupon');
	}


	public function bookmark()
	{
		$this->load->view('profile_bookmark');
	}

}
